//
//  MarryToolViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/4.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZLayout.h"
#import "MarryToolCell.h"

#import "MarryGameSubViewController.h"
#import "MarrySpeechCenterViewController.h"
#import "WedingRegistrationViewController.h"
#import "SeatsListViewController.h"
#import "BudgetHomeVC.h"
#import "BudgetPieVC.h"
#import "ScheduleManagerVC.h"

#import "MarriagePlanViewController.h"
#import "E_invitationSegViewController.h"

#import "MarryToolViewController.h"

@interface MarryToolViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, ZZLayoutDelegate>

@property (nonatomic, strong) NSMutableArray <MarryToolModel *>*dataArray;

@property (nonatomic, strong) NSMutableArray *data;


@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation MarryToolViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self prepareData];
    [self initView];
    
}

- (void)prepareData {
    
    self.dataArray = @[].mutableCopy;
    for (NSDictionary *dic in self.data) {
        
        MarryToolModel *model = [MarryToolModel modelWithDictionary:dic];
        [self.dataArray addObject:model];
    }
    
}

- (void)initView {

    self.title = @"备婚工具";
    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.collectionView reloadData];
}

#pragma mark- 协议方法
//collectionView的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableView = nil;
    // 区头
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader" forIndexPath:indexPath];
        reusableView = headerView;
    }
    // 区尾
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        reusableView = footerView;
    }
    return reusableView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (self.dataArray[indexPath.row].actionType) {
        case MarryToolActionType_dianziqingjian:{
            E_invitationSegViewController *vc = [[E_invitationSegViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case MarryToolActionType_beihunjihua:{
            MarriagePlanViewController *vc = [[MarriagePlanViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case MarryToolActionType_huangdaojiri:{
            ScheduleManagerVC *vc = [[ScheduleManagerVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case MarryToolActionType_hunliyusuanbiao:{
            id obj = [[TTUserModel sharedObject].userModelDefault valueForKey:@"buget_totalMoney"];
            if (obj == nil) {
                BudgetHomeVC *vc = [[BudgetHomeVC alloc]init];
                vc.native = YES;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                BudgetPieVC *vc = [[BudgetPieVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        } break;
        case MarryToolActionType_xiweianpaibiao:{
            SeatsListViewController * vc = [[SeatsListViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case MarryToolActionType_hunyindengjichu:{
            WedingRegistrationViewController *vc = [[WedingRegistrationViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case MarryToolActionType_shiyanzhiciku:{
            MarrySpeechCenterViewController *vc = [[MarrySpeechCenterViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case MarryToolActionType_jieqinyouxi:{
            MarryGameSubViewController *vc = [[MarryGameSubViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        default:
            break;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MarryToolCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MarryToolCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    return cell;
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的高
    return 80;
}

- (NSInteger)layout:(ZZLayout *)layout columnNumberAtSection:(NSInteger)section {//每个区有几列

    return 2;
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距

    return 10;
}

- (CGFloat)layout:(ZZLayout*)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距

    return 10;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {//设置区头的高度

    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForFooterInSection:(NSInteger)section {//设置区尾的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGFloat)layout:(ZZLayout*)layout spacingWithLastSectionForSectionAtIndex:(NSInteger)section {//本区区头和上个区区尾的间距
    return 0;
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {//设置不同分区的不同背景颜色
    return UIColorHex(0xffffff);
}

#pragma mark- 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeVertical delegate:self];
        //打开区头悬浮功能(默认关闭)
        layout.sectionHeadersPinToVisibleBounds = NO;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = UIColorHex(0xffffff);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"MarryToolCell" bundle:nil] forCellWithReuseIdentifier:@"MarryToolCellID"];

        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];

        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;

        _collectionView.bounces = YES;
        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}

- (NSMutableArray <MarryToolModel *>*)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (NSMutableArray *)data {
    NSArray * data = @[
        
        @{@"title": @"电子请柬",
          @"subTitle": @"海量模板免费用",
          @"img": @"tool_dianziqingjian",
          @"actionType": @(MarryToolActionType_dianziqingjian)
        },
        @{@"title": @"备婚计划",
          @"subTitle": @"最全婚礼筹备",
          @"img": @"tool_beihunjihua",
          @"actionType": @(MarryToolActionType_beihunjihua)
        
        },
        @{@"title": @"黄道吉日",
          @"subTitle": @"老黄历选择吉日",
          @"img": @"tool_huangdaojiri",
          @"actionType": @(MarryToolActionType_huangdaojiri)
        
        },
        @{@"title": @"婚礼预算表",
          @"subTitle": @"高性价合理方案",
          @"img": @"tool_hunliyusuanbiao",
          @"actionType": @(MarryToolActionType_hunliyusuanbiao)
        
        },
        @{@"title": @"席位安排表",
          @"subTitle": @"安排宾客",
          @"img": @"tool_xiweianpaibiao",
          @"actionType": @(MarryToolActionType_xiweianpaibiao)
        
        },
        @{@"title": @"婚姻登记处",
          @"subTitle": @"领证快捷方便",
          @"img": @"tool_hunyindengjichu",
          @"actionType": @(MarryToolActionType_hunyindengjichu)
        
        },
        @{@"title": @"誓言致辞库",
          @"subTitle": @"爱的宣言",
          @"img": @"tool_shiyanzhiciku",
          @"actionType": @(MarryToolActionType_shiyanzhiciku)
        
        },
        @{@"title": @"接亲游戏",
          @"subTitle": @"游戏清单",
          @"img": @"tool_jieqinyouxi",
          @"actionType": @(MarryToolActionType_jieqinyouxi)
        
        }
    ];
    return data.modelCopy;
}


@end
